package com.whlilong.jxc.dialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import com.whlilong.jxc.sys.SearchHelpTray;

public class AbstractDialog extends TitleAreaDialog {

	private StringBuffer message = new StringBuffer("武汉市立隆电子科技工程有限公司   电话：027-85506209 13071289833\n");
	private boolean cacel = false;
	protected boolean flag = false;
	
	public int Add_id = 9999;
	public boolean isAdd = true;
	
	public AbstractDialog(Shell parentShell) {
		super(parentShell);
		// TODO Auto-generated constructor stub
	}
	protected Control createDialogArea(Composite parent) {
		// create the top level composite for the dialog area
		setTitle("武汉立隆商务信息管理系统");
//		String initialMsg = message.append("\t选择条件进行查询。。。").toString();
		setMessage(message.toString());
		Composite composite = new Composite(parent, SWT.NONE);
		FormLayout layout = new FormLayout();
		layout.marginTop = 10;
		layout.marginLeft = 5;
		layout.marginRight = 5;
		composite.setLayout(layout);
		composite.setFont(parent.getFont());

		createGroupText(composite);
		return composite;
	}
	
	protected void createButtonsForButtonBar(Composite parent) {
		// create OK and Cancel buttons by default
		createButton(parent, IDialogConstants.OK_ID, "查询(&S)", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "取消(&X)", false);

	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("条件查询");
	}

	protected void buttonPressed(int buttonId) {
		if (IDialogConstants.OK_ID == buttonId) {
			exeSearch();
			okPressed();
		} else if (IDialogConstants.CANCEL_ID == buttonId) {
			cacel  = true;
			cancelPressed();
		}else if(buttonId==9999){
			continueAdd();
		}
	}
	
	protected void continueAdd() {
		
	}
	protected void exeSearch() {
		
	}
	
	protected void okPressed() {
		setReturnCode(OK);
		close();
	}
	
	public Control createGroupText(Composite composite) {
		return null;
	}
	
	public boolean isCacel() {
		return cacel;
	}

	public void setCacel(boolean cacel) {
		this.cacel = cacel;
	}
	
	public boolean isHelpAvailable() {
		return true;
	}

	/*
	 * protected Control createHelpControl(Composite parent) { // TODO
	 * Auto-generated method stub return super.createHelpControl(parent); }
	 */

	protected Control createHelpControl(Composite parent) {
		Image helpImage = JFaceResources.getImage(DLG_IMG_HELP);
		if (helpImage != null) {
			return createHelpImageButton(parent, helpImage);
		}
		return parent;
	}

	/*
	 * Creates a button with a help image. This is only used if there is an
	 * image available.
	 */
	private ToolBar createHelpImageButton(Composite parent, Image image) {
		ToolBar toolBar = new ToolBar(parent, SWT.FLAT | SWT.NO_FOCUS);
		((GridLayout) parent.getLayout()).numColumns++;
		toolBar.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		final Cursor cursor = new Cursor(parent.getDisplay(), SWT.CURSOR_HAND);
		toolBar.setCursor(cursor);
		toolBar.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				cursor.dispose();
			}
		});
		ToolItem item = new ToolItem(toolBar, SWT.NONE);
		item.setImage(image);
		item.setToolTipText(JFaceResources.getString("helpToolTip")); //$NON-NLS-1$
		item.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {
				if (!flag ) {
					openTray(new SearchHelpTray());
					flag = true;
				} else {
					closeTray();
					flag = false;
				}

			}
		});
		return toolBar;
	}


}
